package com.atguigu.tingshu.payment.strategy.impl;

import com.atguigu.tingshu.account.client.RechargeInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.strategy.PaymentStrategy;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RachargePaymentStrategy implements PaymentStrategy {
    @Autowired
    private RechargeInfoFeignClient rechargeInfoFeignClient;

    @Autowired
    private PaymentInfoService paymentInfoService;

    @Autowired
    private RSAAutoCertificateConfig rsaAutoCertificateConfig;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private WxPayV3Config wxPayV3Config;
    @Override
    public WeChatPayVo createJsapi(String paymentType, String orderNo) {
        RechargeInfo rechargeInfo = rechargeInfoFeignClient.getByOrderNo(orderNo).getData();
        if(SystemConstant.ORDER_STATUS_PAID.equals(rechargeInfo.getRechargeStatus())){
            return null ;
        }
        //保存支付信息
        paymentInfoService.savePaymentInfo(rechargeInfo);
        //调用支付下单接口
        WeChatPayVo weChatPayVo = requestWeChatPay(wxPayV3Config, orderNo, "充值", rsaAutoCertificateConfig, userInfoFeignClient);
        return weChatPayVo;
    }

    @Override
    public String getPaymentType() {
        return SystemConstant.PAYMENT_TYPE_RECHARGE;
    }
}
